<?php

namespace App\Http\Controllers\admin;

use App\Http\model\Navs;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavsController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Navs::orderBy('nav_order','asc')->get();
        return view('admin.navs.index')->with('data',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.navs.add');
    }

    /**
     * Store a newly created resource in storage.
     *post.admin/navs
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
        $input = Input::except('_token');
        $rules = [
            'nav_name' => 'required',
        ];
        $message = [
            'nav_name.required' => '导航名称必须填写!',
        ];
        $validator = Validator::make($input, $rules, $message);
        if ($validator->passes()) {
            $row = Navs::create($input);
            if ($row) {
                return redirect('admin/navs');
            } else {
                return back()->with('errors', '添加失败请稍后重试!');
            }
        } else {
            return back()->withErrors($validator);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($nav_id)
    {
        $row = Navs::find($nav_id);
        return view('admin.navs.edit')->with('row',$row);
    }

    /**
     * Update the specified resource in storage.
     *put.admin/links/{links}
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($nav_id)
    {
        $input = Input::except('_method','_token');
        $rules = [
            'nav_name'=>'required',
        ];
        $message = [
            'nav_name.required'=>'导航名称必须填写!',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $model = Navs::find($nav_id);
            $ret = $model->update($input);
            if($ret){
                return redirect('admin/navs');
            }else{
                return back()->with('errors','修改失败,请稍后重试!');
            }
        }else{
            return back()->withErrors($validator);
        }
    }

    //changOrder异步修改排序
    public function changOrder(){
        $input = Input::except('_token');
        $model = Navs::find($input['nav_id']);
        $ret = $model->update(['nav_order'=>$input['nav_ord']]);
        if($ret){
            $data = [
                'error'=>'0',
                'msg'=>'修改成功!'
            ];
        }else{
            $data = [
                'error'=>'1',
                'msg'=>'修改失败!'
             ];

        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($nav_id)
    {

       $row = Navs::where('nav_id',$nav_id)->delete();
        if($row){
            $data = [
                'error'=>'0',
                'msg'=>'删除成功!'
            ];
        }else{
            $data = [
                'error'=>'1',
                'msg'=>'删除失败!'
            ];
        }
        return $data;
    }
}
